import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/custom_tab.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:boss/ui/widget/radioList/radio_item.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

Future<List<Commodity>> showPickerCommodityModal(
  BuildContext context, {
  List<int> selected,
  List<StoreMenu> menus,
}) {
  return showModal<List<Commodity>>(
    context,
    builder: (context) => _PickerCommodityModal(
      selected: selected,
      menus: menus,
    ),
  );
}

class _PickerCommodityModal extends StatefulWidget {
  final List<int> selected;
  final List<StoreMenu> menus;

  const _PickerCommodityModal({this.selected, this.menus});

  @override
  __PickerCommodityModalState createState() => __PickerCommodityModalState();
}

class __PickerCommodityModalState extends State<_PickerCommodityModal> {
  List<StoreMenu> menus = [];

  List<Commodity> selected = [];

  List<String> get tabs => menus.map((item) => item.menuName).toList();

  @override
  void initState() {
    super.initState();
    menus = widget.menus;

    selected = menus[0].commodityList.where((item) {
      return widget.selected.contains(item.id);
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 660.w,
      height: 620.w,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(
          10.w,
        ),
      ),
      padding: EdgeInsets.all(
        30.w,
      ),
      child: Column(
        children: <Widget>[
          Container(
            child: Text(
              '选择菜品',
              style: TextStyleConstant().normal_1_32,
            ),
          ),
          Expanded(
            child: CustomTab(
              tabs: tabs,
              tabViews: menus.map((menu) {
                return _CommodityView(
                  menuId: menu.menuId,
                  commoditys: menu.commodityList,
                  selected: selected,
                  onChanged: (value) {
                    setState(() {
                      selected = value;
                    });
                  },
                );
              }).toList(),
            ),
          ),
          RadiusButton(
            width: double.infinity,
            title: '确定',
            onTap: () {
              Navigator.pop(context, selected);
            },
          ),
        ],
      ),
    );
  }
}

class _CommodityView extends StatelessWidget {
  final List<Commodity> commoditys;
  final List<Commodity> selected;
  final ValueChanged<List<Commodity>> onChanged;
  final int menuId;

  const _CommodityView({
    Key key,
    this.commoditys,
    this.selected,
    this.onChanged,
    this.menuId,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.all(0),
      itemBuilder: (context, index) {
        if (index == 0) {
          bool isSelectedAll;
          if (menuId == -1) {
            isSelectedAll = commoditys.length == selected.length;
          } else {
            isSelectedAll = commoditys.length == selected.where((item) => item.menuId == menuId).length;
          }

          return RadioItem(
            title: '全部',
            isSelected: isSelectedAll,
            onTap: () {
              if (isSelectedAll) {
                commoditys.forEach((item) {
                  selected.remove(item);
                });
                onChanged(selected);
              } else {
                selected.addAll(commoditys);
                var temp = Set<Commodity>.from(selected).toList();
                onChanged(temp);
              }
            },
          );
        }

        var current = commoditys[index - 1];
        var isSelected = selected.contains(current);

        return RadioItem(
          title: current.title,
          isSelected: isSelected,
          onTap: () {
            isSelected ? selected.remove(current) : selected.add(current);
            onChanged(selected);
          },
        );
      },
      itemCount: commoditys.length + 1,
    );
  }
}
